# -*- coding: utf-8 -*-
# ----------------------------
# @Author:    影子
# @Software:  PyCharm
# @时间:       2024/8/22 下午2:38
# @项目:       chengxu
# @FileName:  logs.py
# ----------------------------
from time import strftime
from time import localtime
import logging
from logging.handlers import TimedRotatingFileHandler

from common.paths import pathlibs


class MyLogger(object):
    """日志类"""

    @staticmethod
    def create_logger():
        """日志配置"""
        my_logger = logging.getLogger("my_logger")
        my_logger.setLevel("DEBUG")
        # 控制台处理器
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel("DEBUG")
        my_logger.addHandler(stream_handler)
        # 使用时间滚动的文件处理器
        log_file_handler = TimedRotatingFileHandler(
            filename=f'{pathlibs("logs")}{strftime("%Y-%m-%d", localtime())}.log', when='D', backupCount=10,
            encoding='utf-8')
        log_file_handler.setLevel("DEBUG")
        my_logger.addHandler(log_file_handler)
        formatter = logging.Formatter('%(asctime)s - [%(filename)s-->line:%(lineno)d] - %(levelname)s: %(message)s')
        stream_handler.setFormatter(formatter)
        log_file_handler.setFormatter(formatter)
        return my_logger


logger = MyLogger().create_logger()

if __name__ == '__main__':
    logger.info("info")
    logger.error("error")
    logger.warning("warning")
    logger.debug("debug\n")

